package com.infra.micro.oss.domain.service.impl;

import com.infra.common.resp.Response;
import com.infra.micro.oss.domain.db.entity.BizInfoPO;
import com.infra.micro.oss.domain.db.entity.FileInfoPO;
import com.infra.micro.oss.domain.service.PreviewService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

@Service
public class PreviewServiceImpl extends BaseFileService implements PreviewService {

    /**
     * 预览文件
     *
     * @param fileId
     * @param response
     * @throws Exception
     */
    public void previewByFileId(Long fileId, HttpServletResponse response) throws Exception {
        FileInfoPO fileInfoPO = fileInfoRepository.queryByFileId(fileId);
        if (fileInfoPO == null) {
            rewrite404Response(response, Response.error("文件ID无效,文件未找到"));
            return;
        }

        response.setContentType(fileInfoPO.getMimeType());
        try {
            writeOutputStream(ossClient.getFileObject(fileId.toString()), response);
        } catch (Exception e) {
            LOGGER.error("预览缩略图时出与异常", e);
            rewrite404Response(response, Response.error(e.getMessage()));
        }

    }

    /**
     * 预览文件
     *
     * @param bizId
     * @param response
     * @throws Exception
     */
    public void previewByBizId(Long bizId, HttpServletResponse response) throws Exception {
        BizInfoPO bizInfoPO = bizInfoRepository.queryByBizId(bizId);
        if (bizInfoPO == null) {
            // 不存在的情况下是否考虑下载一张默认不存在的提示图；
            rewrite404Response(response, Response.error("业务ID无效,文件未找到"));
            return;
        }

        previewByFileId(bizInfoPO.getFileId(), response);
    }

    public void previewThumbnailByFileId(Long fileId, String thumbnail, HttpServletResponse response) throws Exception {
        FileInfoPO fileInfoPO = fileInfoRepository.queryByFileId(fileId);
        if (fileInfoPO == null) {
            rewrite404Response(response, Response.error("文件ID无效,文件未找到"));
            return;
        }
        if (StringUtils.isBlank(fileInfoPO.getThumbnail()) || !fileInfoPO.getThumbnail().contains(thumbnail)) {
            rewrite404Response(response, Response.error("缩略图尺寸未找到"));
            return;
        }

        response.setContentType(fileInfoPO.getMimeType());
        try {
            writeOutputStream(ossClient.getFileObject(String.format(THUMBNAIL_FORMAT_KEY, fileId.toString(), thumbnail)), response);
        } catch (Exception e) {
            LOGGER.error("预览缩略图时出与异常", e);
            rewrite404Response(response, Response.error(e.getMessage()));
        }

    }

    public void previewThumbnailByBizId(Long bizId, String thumbnail, HttpServletResponse response) throws Exception {
        BizInfoPO bizInfoPO = bizInfoRepository.queryByBizId(bizId);
        if (bizInfoPO == null) {
            // 不存在的情况下是否考虑下载一张默认不存在的提示图；
            rewrite404Response(response, Response.error("业务ID无效,文件未找到"));
            return;
        }
        previewThumbnailByFileId(bizInfoPO.getFileId(), thumbnail, response);
    }
}
